package by.dkusch.aircompany.webapp.page.flight.panel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.inject.Inject;

import org.apache.wicket.Page;
import org.apache.wicket.Session;
import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.event.IEvent;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.PageCreator;
import org.apache.wicket.extensions.yui.calendar.DateTimeField;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.ResourceModel;

import by.dkusch.aircompany.model.tables.Crew;
import by.dkusch.aircompany.model.tables.Flight;
import by.dkusch.aircompany.model.tables.Plane;
import by.dkusch.aircompany.model.tables.Route;
import by.dkusch.aircompany.services.CrewService;
import by.dkusch.aircompany.services.FlightService;
import by.dkusch.aircompany.services.PlaneService;
import by.dkusch.aircompany.services.RouteService;
import by.dkusch.aircompany.webapp.page.base.enums.ActionType;
import by.dkusch.aircompany.webapp.page.base.enums.SelectorType;
import by.dkusch.aircompany.webapp.page.base.events.SelectorDirectionEvent;
import by.dkusch.aircompany.webapp.page.staff.panel.CrewListPanel;
import by.dkusch.aircompany.webapp.page.tech.panel.PlaneListPanel;

public class FlightEditPanel extends Panel {

	private static final long serialVersionUID = 1L;
	@Inject
	private FlightService flightService;

	@Inject
	private RouteService routeService;

	@Inject
	private PlaneService planeService;

	@Inject
	private CrewService crewService;

	private Locale selectedLocale;

	public FlightEditPanel(String id, final Flight flight, final PageCreator pageCreator, ActionType action) {
		super(id);

		selectedLocale = Session.get().getLocale();

		Form<Flight> form = new Form<Flight>("form", new CompoundPropertyModel<Flight>(flight));

		final RequiredTextField<String> tfRoute = new RequiredTextField<String>("route.code") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onEvent(IEvent<?> event) {
				Object payload = event.getPayload();
				if (payload instanceof SelectorDirectionEvent<?>) {
					SelectorDirectionEvent<?> selectorEvent = (SelectorDirectionEvent<?>) payload;
					if (selectorEvent.getSelector().equals(SelectorType.ROUTE)) {
						Route route = routeService.get((Long) selectorEvent.getEntityID());
						flight.setRoute(route);
						setDefaultModelObject(route != null ? route.getCode() : getString("air.error.detail.empty"));
						selectorEvent.getTarget().add(this);
					}
				}
			}
		};
		tfRoute.add(new PropertyValidator<String>()).setLabel(new ResourceModel("flight.list.route.code")).setEnabled(false);
		form.add(tfRoute);

		final TextField<String> tfPlane = new TextField<String>("plane.code") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onEvent(IEvent<?> event) {
				Object payload = event.getPayload();
				if (payload instanceof SelectorDirectionEvent<?>) {
					SelectorDirectionEvent<?> selectorEvent = (SelectorDirectionEvent<?>) payload;
					if (selectorEvent.getSelector().equals(SelectorType.PLANE)) {
						Plane plane = planeService.get((Long) selectorEvent.getEntityID());
						flight.setPlane(plane);
						setDefaultModelObject(plane != null ? plane.getCode() : getString("air.error.detail.empty"));
						selectorEvent.getTarget().add(this);
					}
				}
			}

		};

		tfPlane.add(new PropertyValidator<String>()).setLabel(new ResourceModel("flight.list.plane.code")).setEnabled(false);
		form.add(tfPlane);

		final TextField<String> tfCrew = new TextField<String>("crew.code") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onEvent(IEvent<?> event) {
				Object payload = event.getPayload();
				if (payload instanceof SelectorDirectionEvent<?>) {
					SelectorDirectionEvent<?> selectorEvent = (SelectorDirectionEvent<?>) payload;
					if (selectorEvent.getSelector().equals(SelectorType.CREW)) {
						Crew crew = crewService.get((Long) selectorEvent.getEntityID());
						flight.setCrew(crew);
						setDefaultModelObject(crew != null ? crew.getCode() : getString("air.error.detail.empty"));
						selectorEvent.getTarget().add(this);
					}
				}
			}

		};

		tfCrew.add(new PropertyValidator<String>()).setLabel(new ResourceModel("flight.list.crew.code")).setEnabled(false);
		form.add(tfCrew);

		final DateTimeField tfStartDate = new DateTimeField("startTime") {

			private static final long serialVersionUID = 1L;

			@Override
			public Locale getLocale() {
				return selectedLocale;
			}
		};
		tfStartDate.add(new PropertyValidator<Date>()).setLabel(new ResourceModel("flight.list.departure"));
		form.add(tfStartDate);

		final DateTimeField tfEndDate = new DateTimeField("endTime") {

			private static final long serialVersionUID = 1L;

			@Override
			public Locale getLocale() {
				return selectedLocale;
			}
		};
		tfEndDate.add(new PropertyValidator<Date>()).setLabel(new ResourceModel("flight.list.arrival"));
		form.add(tfEndDate);

		final TextField<String> tfPS1 = new TextField<String>("passClass1");
		tfPS1.add(new PropertyValidator<String>()).setLabel(new ResourceModel("flight.list.ps1"));
		form.add(tfPS1);

		final TextField<String> tfPS2 = new TextField<String>("passClass2");
		tfPS2.add(new PropertyValidator<String>()).setLabel(new ResourceModel("flight.list.ps2"));
		form.add(tfPS2);

		final TextField<String> tfPS3 = new TextField<String>("passClass3");
		tfPS3.add(new PropertyValidator<String>()).setLabel(new ResourceModel("flight.list.ps3"));
		form.add(tfPS3);

		final TextField<String> tfDescription = new TextField<String>("description");
		tfDescription.add(new PropertyValidator<String>()).setLabel(new ResourceModel("flight.list.description"));
		form.add(tfDescription);

		final CheckBox chBArc = new CheckBox("arcFl");
		chBArc.add(new PropertyValidator<Boolean>()).setLabel(new ResourceModel("flight.list.arcfl"));
		form.add(chBArc);

		form.add(new Label("correctDt"));

		final SubmitLink submitLink = new SubmitLink("sumbit-link") {

			private static final long serialVersionUID = 1L;
			private List<String> errorMessage = new ArrayList<String>();;

			@Override
			public void onSubmit() {
				super.onSubmit();
				errorMessage.clear();
				if (flight.getRoute() == null) {
					errorMessage.add(getString("RouteEmptyMessage"));
				}
				if (flight.getCrew() == null) {
					errorMessage.add(getString("CrewEmptyMessage"));
				}
				if (flight.getPlane() == null) {
					errorMessage.add(getString("PlaneEmptyMessage"));
				}
				if (errorMessage.size() > 0) {
					onError();
					return;
				}
				flightService.saveOrUpdate(flight);

				Page page = pageCreator.createPage();
				page.info(getString("column.common.action.saved"));
				setResponsePage(page);
			}

			@Override
			public void onError() {
				for (String msg : errorMessage) {
					error(msg);
				}
			}

		};
		submitLink.setOutputMarkupId(true);
		form.add(submitLink);

		add(new Link<Void>("back-link") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				setResponsePage(pageCreator.createPage());
			}

		});

		CrewListPanel crewListPanel = new CrewListPanel("crew-panel", 5, true);
		form.add(crewListPanel.setVisible(action.equals(ActionType.EDIT)));

		PlaneListPanel planeListPanel = new PlaneListPanel("plane-panel", 5, true);
		form.add(planeListPanel.setVisible(action.equals(ActionType.EDIT)));

		RouteListPanel routeListPanel = new RouteListPanel("route-panel", 5, true);
		form.add(routeListPanel.setVisible(action.equals(ActionType.EDIT)));

		add(form.setEnabled(action.equals(ActionType.EDIT)));

	}

}
